package com.abl.controller.demo;

import com.abl.common.util.ContextKit;
import com.abl.service.system.SysAPIService;
import com.abl.service.system.SysDictService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

@Component
public class Demo implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

    }

    //测试多线程占用情况
    private void testManyThread() {
        ArrayList<FutureTask<Object>> objects = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            FutureTask<Object> futureTask = new FutureTask<>(new Callable<Object>() {
                private long a = 100000;

                @Override
                public Object call() throws Exception {
                    while (true) {
                        a += 1;
                        if (a > 2000000) {
                            System.out.println(Thread.currentThread().getName() + "循环一次结束");
                            a = 100000;
                        }
//                        HashMap<String, ArrayList<HashMap<String, Object>>> stringArrayListHashMap = ResAnnotationHandler.scanRes();
//                        Thread.sleep(1000);
//                        System.out.println(Thread.currentThread().getName());
                    }
                }
            });
            objects.add(futureTask);
        }
        for (FutureTask<Object> futureTask : objects) {
            ExecutorService executor = Executors.newScheduledThreadPool(50);
            executor.submit(futureTask);
        }
        //        for (FutureTask<Object> futureTask : objects) {
        //            futureTask.get();
        //        }
    }

    private void testSysDict() throws InterruptedException {

        //插入新资源
        SysAPIService res = ContextKit.getBean(SysAPIService.class);

        //刷新字典缓存
        SysDictService dict = ContextKit.getBean(SysDictService.class);

    }

    //    Transactional标注了的方法，内部异常必须抛出来才能回滚（异常必须经过方法，才能被方法上的注解拦截到）
    //    @Transactional(rollbackFor = Exception.class)
    //    @Transactional
    //    @Override
    //    public SRS<Object> testTrans() throws Exception {
    //
    //        Dnt dnt = new Dnt();
    //        int id = 2999;
    //        dnt.set("id", id);
    //        dnt.set("type", "ooop" + id);
    //        dnt.set("code", "ooop" + id);
    //        long insert = dao.insert("sys_dict", dnt);
    //        try {
    //            System.out.println(0 / 0);
    //        } catch (Exception e) {
    //            System.out.println("88888888888888888");
    //        }
    //        return SRS.bySuccess(insert);
    //    }


}
